package com.sikaryofficial.gateway.filter;

import com.sikaryofficial.common.core.utils.ServletUtils;
import com.sikaryofficial.gateway.config.properties.AIProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;

/**
 * 读取 ai.enabled 配置的参数，判断是否开启 AI 功能
 */
@Component
@Slf4j
public class AIFilter extends AbstractGatewayFilterFactory<Object> {

    @Autowired
    private AIProperties aiProperties;

    @Override
    public GatewayFilter apply(Object config) {
        return (exchange, chain) -> {
            log.info("AI filter");
            if (Boolean.TRUE.equals(aiProperties.getEnabled())) {
                return chain.filter(exchange);
            }
            log.info("AI not enabled");
            return ServletUtils.webFluxResponseWriter(exchange.getResponse(), "AI dialogue function not enabled");
        };
    }
}
